package com.qiche.controller.mgr;

import com.github.pagehelper.PageInfo;
import com.qiche.aspect.Result;
import com.qiche.dto.ProductDto;
import com.qiche.enums.ResultEnum;
import com.qiche.exception.MySystemException;
import com.qiche.model.*;
import com.qiche.service.*;
import com.qiche.util.UserUtils;
import com.qiche.vo.ComboVo;
import com.sun.org.apache.regexp.internal.RE;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 * 套餐管理控制器
 * Created by Linzw on 2018/01/24.
 */
@Api(value = "/combos", description = "套餐管理控制器")
@RestController
@RequestMapping("/combos")
public class ComboController {

    @Autowired
    private ComboMasterService comboMasterService;
    @Autowired
    private ComboDetailService comboDetailService;

    @Autowired
    private ProductService productService;


    @ApiOperation(value = "根据条件和分页获取套餐列表")
    @GetMapping
    public Map<String, Object> getAll(ComboMaster comboMaster,
                                      @RequestParam(required = false, defaultValue = "1") int page,
                                      @RequestParam(required = false, defaultValue = "10") int limit) {
        Map<String, Object> map = new HashMap<>();
        PageInfo<ComboMaster> pageInfo = comboMasterService.selectByPage(comboMaster, page, limit);
        map.put("code", 0);
        map.put("msg", "查询成功");
        map.put("count", pageInfo.getTotal());
        map.put("data", pageInfo.getList());
        return map;
    }


    @Transactional
    @ApiOperation(value = "添加套餐")
    @PostMapping
    public Result add(@RequestBody ComboVo comboVo) {
        ComboMaster comboMaster = comboVo.getComboMaster();

        comboMaster.setMerchantId(UserUtils.getCurrentUser().getMerchantId().longValue());
        comboMaster.setCode(System.currentTimeMillis() + "");
        int saveCount = comboMasterService.save(comboMaster);
        if (saveCount > 0) {
            List<ComboDetail> comboDetailList = comboVo.getComboDetailList();
            for (ComboDetail comboDetail : comboDetailList) {
                comboDetail.setMasterId(comboMaster.getId());
            }
            saveCount = comboDetailService.batchInsert(comboDetailList);
        }
        if (saveCount > 0) {
            return Result.createBySuccessMessage("添加成功");
        }
        return Result.createByErrorMessage("添加失败");
    }

    @ApiOperation(value = "更新套餐")
    @PutMapping
    public Result update(@RequestBody ComboVo comboVo) {
        ComboMaster comboMaster = comboVo.getComboMaster();
        int updateCount=  comboMasterService.updateNotNull(comboMaster);
        //根据masterId删除
        comboDetailService.batchDeleteByMasterId(comboMaster.getId().intValue());
        if (updateCount > 0) {
            List<ComboDetail> comboDetailList = comboVo.getComboDetailList();
            for (ComboDetail comboDetail : comboDetailList) {
                comboDetail.setMasterId(comboMaster.getId());
            }
            updateCount = comboDetailService.batchInsert(comboDetailList);
        }
        if (updateCount > 0) {
            return Result.createBySuccessMessage("更新成功");
        }
        return Result.createByErrorMessage("更新失败");
    }


    @ApiOperation(value = "根据ID删除套餐信息")
    @DeleteMapping(value = "/delete/{id}")
    public Result<String> delete(@PathVariable("id") Integer masterId) {
        int deleteCount = comboMasterService.delete(masterId);
        if (deleteCount > 0) {
            deleteCount = comboDetailService.batchDeleteByMasterId(masterId);
        }
        if (deleteCount > 0) {
            return Result.createBySuccessMessage("删除成功");
        } else {
            return Result.createByErrorMessage("删除失败");
        }

    }

    @ApiOperation(value = "根据ID获取套餐信息")
    @GetMapping(value = "/get/{id}")
    public Result<ComboVo> get(@PathVariable("id") Integer masterId) {
        ComboMaster comboMaster = comboMasterService.selectByKey(masterId);
        List<ComboDetail> comboDetailList = comboDetailService.selectByMasterId(comboMaster.getId().intValue());
        for (ComboDetail comboDetail:comboDetailList
             ) {
            comboDetail.setProductName(productService.selectByKey(comboDetail.getProductId()).getName());
        }

        ComboVo comboVo = new ComboVo();
        comboVo.setComboMaster(comboMaster);
        comboVo.setComboDetailList(comboDetailList);
        return Result.createBySuccess(comboVo);
    }

}
